home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / souper15.zip / GETOPT.C < prev    next >
Text File  |  1993-02-03  |  3KB  |  88 lines

  1. /* ::[[ @(#) getopt.c 1.5 89/03/11 05:40:23 ]]:: */
  2. #ifndef LINT
  3. static char sccsid[]="::[[ @(#) getopt.c 1.5 89/03/11 05:40:23 ]]::";
  4. #endif
  5.  
  6. /*
  7.  * Here's something you've all been waiting for:  the AT&T public domain
  8.  * source for getopt(3).  It is the code which was given out at the 1985
  9.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  10.  * directly from AT&T.  The people there assure me that it is indeed
  11.  * in the public domain.
  12.  *
  13.  * There is no manual page.  That is because the one they gave out at
  14.  * UNIFORUM was slightly different from the current System V Release 2
  15.  * manual page.  The difference apparently involved a note about the
  16.  * famous rules 5 and 6, recommending using white space between an option
  17.  * and its first argument, and not grouping options that have arguments.
  18.  * Getopt itself is currently lenient about both of these things White
  19.  * space is allowed, but not mandatory, and the last option in a group can
  20.  * have an argument.  That particular version of the man page evidently
  21.  * has no official existence, and my source at AT&T did not send a copy.
  22.  * The current SVR2 man page reflects the actual behavor of this getopt.
  23.  * However, I am not about to post a copy of anything licensed by AT&T.
  24.  */
  25.  
  26. #ifdef ANSIPROTO
  27. int strcmp (char *, char *);
  28. char *strchr (char *, char);
  29. #endif
  30.  
  31. #include <stdio.h>
  32.  
  33. #define ERR(szz,czz) if(opterr){fprintf(stderr,"%s%s%c\n",argv[0],szz,czz);}
  34.  
  35. extern int strcmp();
  36. extern char *strchr();
  37.  
  38. int   opterr = 1;
  39. int   optind = 1;
  40. int   optopt;
  41. char  *optarg;
  42.  
  43. int
  44. getopt(argc, argv, opts)
  45. int   argc;
  46. char  **argv, *opts;
  47. {
  48.    static int sp = 1;
  49.    register int c;
  50.    register char *cp;
  51.  
  52.    if(sp == 1)
  53.       if(optind >= argc ||
  54.          argv[optind][0] != '-' || argv[optind][1] == '\0')
  55.          return(EOF);
  56.       else if(strcmp(argv[optind], "--") == 0) {
  57.          optind++;
  58.          return(EOF);
  59.       }
  60.    optopt = c = argv[optind][sp];
  61.    if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  62.       ERR(": illegal option -- ", c);
  63.       if(argv[optind][++sp] == '\0') {
  64.          optind++;
  65.          sp = 1;
  66.       }
  67.       return('?');
  68.    }
  69.    if(*++cp == ':') {
  70.       if(argv[optind][sp+1] != '\0')
  71.          optarg = &argv[optind++][sp+1];
  72.       else if(++optind >= argc) {
  73.          ERR(": option requires an argument -- ", c);
  74.          sp = 1;
  75.          return('?');
  76.       } else
  77.          optarg = argv[optind++];
  78.       sp = 1;
  79.    } else {
  80.       if(argv[optind][++sp] == '\0') {
  81.          sp = 1;
  82.          optind++;
  83.       }
  84.       optarg = NULL;
  85.    }
  86.    return(c);
  87. }
  88.